<template>
  <div class="global-box">
    <div class="title">
      <h1>{{ title }}</h1>
      <a-button type="primary" @click="add()">添加协议条款</a-button>
    </div>
    <a-card :body-style="{ padding: '24px' }" :bordered="false">
      <div class="form-wrapper">
        <a-form layout="inline" :labelCol="{ span: 7 }" :wrapperCol="{ span: 17 }">
          <a-row>
            <a-col :md="6" :sm="24">
              <a-form-item label="课程目录">
                <a-cascader
                  :field-names="{ label: 'value', value: 'id', children: 'children' }"
                  :options="directoryOptions"
                  :load-data="loadData"
                  placeholder="请选择课程目录"
                  change-on-select
                  v-model="queryParam.directory"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="条款分类">
                <a-select v-model="queryParam.type" placeholder="请选择" default-value="0">
                  <a-select-option :value="item.value" :key="item.value" v-for="item in typeList">{{
                    item.label
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="条款名称">
                <a-input v-model="queryParam.name" placeholder="请输入条款名称" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="条款ID">
                <a-input v-model="queryParam.id" placeholder="请输入条款ID" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="启用状态">
                <a-select v-model="queryParam.status" placeholder="请选择" default-value="0">
                  <a-select-option
                    :value="item.value"
                    :key="item.value"
                    v-for="item in statusList"
                    >{{ item.label }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="18" :sm="24">
              <span class="table-page-search-submitButtons" style="float: right">
                <a-button type="primary" @click="search">筛选</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-wrapper">
        <a-table :columns="columns" :data-source="data" :pagination="pagination">
          <span slot="clauseCategory" slot-scope="text, record">
            <template>
              <span v-if="record.clauseCategory === 1">基本条款</span>
              <span v-if="record.clauseCategory === 2">完课比例</span>
              <span v-if="record.clauseCategory === 3">考试成绩</span>
              <span v-if="record.clauseCategory === 4">退费/重修其他要求</span>
              <span v-if="record.clauseCategory === 5">退费/重修申请期限</span>
              <span v-if="record.clauseCategory === 6">退费标准</span>
              <span v-if="record.clauseCategory === 7">重修年份</span>
              <span v-if="record.clauseCategory === 8">退费/重新申请材料</span>
            </template>
          </span>
          <span slot="enable" slot-scope="text, record">
            <template>
              {{ record.enable === 1 ? '启用' : '禁用' }}
            </template>
          </span>
          <span slot="action" slot-scope="text, record">
            <template>
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a @click="handleDelete(record)">删除</a>
              <a-divider type="vertical" />
              <a-popconfirm
                placement="top"
                ok-text="确认"
                cancel-text="取消"
                @confirm="handleToggle(record)"
                v-if="record.enable === 1"
              >
                <template slot="title">
                  <p>你确定要禁用该课程吗？</p>
                </template>
                <a>禁用</a>
              </a-popconfirm>
              <a @click="handleToggle(record)" v-else>启用</a>
            </template>
          </span>
        </a-table>
      </div>
    </a-card>
  </div>
</template>

<script>
import { directory, list, deletes, setActive } from '@/api/agreement/clause'
const columns = [
  {
    title: '协议条款ID',
    dataIndex: 'id',
    align: 'center'
  },
  {
    title: '条款名称',
    dataIndex: 'clauseName',
    align: 'center'
  },
  {
    title: '条款分类',
    dataIndex: 'clauseCategory',
    scopedSlots: { customRender: 'clauseCategory' },
    align: 'center'
  },
  {
    title: '所属事业部',
    dataIndex: 'deptName',
    align: 'center'
  },
  {
    title: '条款内容',
    dataIndex: 'clauseContent',
    align: 'center'
  },
  {
    title: '操作人',
    dataIndex: 'creatorName',
    align: 'center'
  },
  {
    title: '操作时间',
    dataIndex: 'createTime',
    align: 'center'
  },
  {
    title: '启用状态',
    dataIndex: 'enable',
    scopedSlots: { customRender: 'enable' },
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center'
  }
]
export default {
  data () {
    return {
      title: '协议条款管理',
      queryParam: {
        directory: [],
        type: undefined,
        name: '',
        id: '',
        status: undefined
      },
      statusList: [
        {
          label: '启用',
          value: '1'
        },
        {
          label: '禁用',
          value: '0'
        }
      ],
      typeList: [{
        label: '基本条款',
        value: 1
      }, {
        label: '完课比例',
        value: 2
      }, {
        label: '考试成绩',
        value: 3
      }, {
        label: '退费/重修其他要求',
        value: 4
      }, {
        label: '退费/重修申请期限',
        value: 5
      }, {
        label: '退费标准',
        value: 6
      }, {
        label: '重修年份',
        value: 7
      }, {
        label: '退费/重新申请材料',
        value: 8
      }],
      columns,
      data: [],
      pagination: {
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        showSizeChanger: true,
        showQuickJumper: true,
        onShowSizeChange: (current, pageSize) => {
          this.pagination.defaultCurrent = 1
          this.pagination.defaultPageSize = pageSize
          this.getData()
        },
        // 改变每页数量时更新显示
        onChange: (current, size) => {
          this.pagination.defaultCurrent = current
          this.pagination.defaultPageSize = size
          this.getData()
        }
      },
      directoryOptions: [],
      labelCol: { span: 10 },
      wrapperCol: { span: 14 }
    }
  },
  computed: {
  },
  mounted () {
    this.getDirectory()
    this.getData()
  },
  methods: {
    // 获取列表数据
    getData () {
      const data = {
        currentPage: this.pagination.defaultCurrent,
        pageSize: this.pagination.defaultPageSize,
        clauseCategory: this.queryParam.type || 0,
        enable: 2,
        clauseId: this.queryParam.id || 0,
        clauseName: this.queryParam.name || '',
        directoryId: this.queryParam.directory[0] || 0,
        examId: this.queryParam.directory[1] || 0
      }
      if (this.queryParam.status) {
        data.enable = Number(this.queryParam.status)
      }
      list(data).then(res => {
        if (res.code === '1') {
          this.pagination.total = res.data.totalSize
          this.data = res.data.rows
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 筛选
    search () {
      this.getData()
    },
    // 重置
    reset () {
      this.queryParam = {
        directory: [],
        type: undefined,
        name: '',
        id: '',
        status: undefined
      }
      this.getData()
    },
    // 添加协议条款
    add () {
      this.$router.push({ name: 'clauseCommon' })
    },
    // 表格编辑
    handleEdit (record) {
      if (record.ifEdit !== 0) {
        this.$message.error('该协议条款已关联协议模板，无法编辑修改')
        return false
      }
      this.$router.push({
        name: 'clauseCommon',
        params: {
          re: record
        }
      })
      // console.log(record)
    },
    // 表格刪除
    handleDelete (record) {
      console.log(record)
      const data = {
        id: record.id
      }
      deletes(data).then(res => {
        if (res.code === '1') {
          this.$message.success(res.msg)
          this.getData()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 表格启用禁用
    handleToggle (record) {
      console.log(record)
      const data = {
        id: record.id,
        enable: record.enable === 0 ? 1 : 0
      }
      setActive(data).then(res => {
        if (res.code === '1') {
          this.$message.success(res.msg)
          this.getData()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 获取课程目录
    getDirectory () {
      const data = {
        type: 0,
        directoryLevel: 1
      }
      directory(data).then(res => {
        if (res.code === '1') {
          res.data.forEach(item => {
            item.isLeaf = false
          })
          this.directoryOptions = res.data
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 课程目录二级-六级
    loadData (selectedOptions) {
      if (this.queryParam.directory[1]) {
        return false
      }
      const targetOption = selectedOptions[selectedOptions.length - 1]
      const data = {
        type: 1,
        parentId: targetOption.id,
        directoryLevel: 2
      }
      targetOption.loading = true
      directory(data).then(res => {
        if (res.code === '1') {
          if (res.data.length > 0) {
            res.data.forEach(item => {
              item.isLeaf = false
            })
            targetOption.loading = false
            targetOption.children = [...res.data]
          } else {
            targetOption.loading = false
            this.$message.error('暂无更多数据')
          }
        } else {
          targetOption.loading = false
          this.$message.error(res.msg)
        }
        this.directoryOptions = [...this.directoryOptions]
      }).catch(err => {
        this.$message.error(err.msg)
        targetOption.loading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.form-status-tip {
  text-align: center;
  color: #999;
}
</style>
